package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.javabean.AddDevice;
import com.model.TangDBUtil;

/**
 * Servlet implementation class totalchuru
 */
@WebServlet("/files/totalchuru")
public class totalchuru extends HttpServlet {
	AddDevice test = new AddDevice();
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public totalchuru() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String[] param = new String[0];
		int[] which = new int[0];
		String countsqlString = "select count(*) as B from borrow_record";
		String rssqlString = "select * from borrow_record,device where DeviceId=did";
		int requestPage = Integer.parseInt(request.getParameter("requestPage")); // 得到当前页面是第几页
		try {
			TangDBUtil connectDB = new TangDBUtil();
			ResultSet rstotalRecord = connectDB.executeQuery(countsqlString,
					param, 0, which);
			int totalrecord = 0;
			int totalPage = 0; // 获取总共的页数
			if (rstotalRecord.next()) {
				totalrecord = rstotalRecord.getInt("B");
				totalPage = totalrecord / 10 + 1;
			}
			ResultSet rsdev = connectDB.executeQuery(rssqlString, param, 0,
					which);
			int i = 0;
			if (requestPage != 1) {
				while (rsdev.next()) {
					i++;
					if (i > ((requestPage - 1) * 9 - 1))
						break;
				}
			}

			// Json赋值
			int k = 0;
			JSONObject json = new JSONObject();
			JSONArray array = new JSONArray();
			JSONObject member = null;
			while (rsdev.next()) {
				k++;
				if (k > 9) {
					break;
				}
				member = new JSONObject();
				member.put("date", rsdev.getString("BorrowDate"));
				member.put("name", rsdev.getString("type"));
				member.put("devserial", rsdev.getString("serial"));
				member.put("position", rsdev.getString("department"));
				member.put("username", test.getUserName(rsdev.getLong("BorrowerId")));
				member.put("inout", rsdev.getString("BorrowOrReturn"));

				// member.put("devvender", rsdev.getString("vender"));
				// member.put("devmdate", rsdev.getString("mdate"));
				// member.put("devedate", rsdev.getString("edate"));
				// member.put("depart", rsdev.getString("department"));
				// member.put("devremark", rsdev.getString("remark"));

				// member.put("did", rsdev.getString("did"));
				array.add(member);
			}
			member = new JSONObject();
			member.put("totalSet", totalrecord);
			member.put("totalPage", totalPage);
			member.put("curPage", requestPage); // 发送的第几页
			array.add(member);
			json.put("dev", array);
			out.print(json.toString());
			connectDB.closeConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
